﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Hosting.Class
{
    public class OrderDetail
    {

        public OrderDetail() { }

        #region [Get all Order Detail]
        /// <summary>
        /// Get all Order Detail by Order ID
        /// </summary>
        /// <param name="oId">ID of Order</param>
        /// <returns>
        /// <para>Return list of Order Detail by Order ID</para>
        /// <para>null      : error</para>
        /// </returns>
        public List<PC_OrderDetails_GetByOrderIDResult> getByOrderID(string oId)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var list = db.PC_OrderDetails_GetByOrderID(Int32.Parse(oId)).ToList();
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Get one Order Detail]
        /// <summary>
        /// Get one Order Detail
        /// </summary>
        /// <param name="oId">ID of Order Detail</param>
        /// <returns>
        /// <para>Return list of Order Detail</para>
        /// <para>null      : error</para>
        /// </returns>
        public List<PC_OrderDetails_GetOneResult> getOne(string id)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var list = db.PC_OrderDetails_GetOne(Int32.Parse(id)).ToList();
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Insert Order Detail]
        /// <summary>
        /// Insert Order Detail
        /// </summary>
        /// <param name="oId">ID of Order</param>
        /// <param name="detail">Detail about Service Package, Service Category, Service Category Title and Service Category Detail</param>
        /// <returns>
        /// <para>1     : successfully</para>
        /// <para>0     : Order contains this Order Detail has been done, you cannot insert another Order Detail</para>
        /// <para>-99   : error</para>
        /// </returns>
        public int add(string oId, string detail)
        {
            try
            {
                int? isInsert = 0;
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isSuccess = db.PC_OrderDetails_Ins(Int32.Parse(oId), detail, ref isInsert);
                if (isInsert == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check insert is valid or not]
        /// <summary>
        /// Check insert is valid or not
        /// </summary>
        /// <param name="oId">ID of Order contains this Order Detail</param>
        /// <returns>
        /// <para>1     : successfully</para>
        /// <para>2     : Order ID is null or empty</para>
        /// <para>3     : Order ID is not valid</para>
        /// </returns>
        public int addValid(string oId)
        {
            if (String.IsNullOrEmpty(oId))
            {
                return 2;
            }

            Regex rOId = new Regex("^[0-9]{0,}$");
            Match mOId = rOId.Match(oId);
            if (!mOId.Success)
            {
                return 3;
            }

            return 1;
        }
        #endregion

        #region [Update Order Detail]
        /// <summary>
        /// Update Order Detail
        /// </summary>
        /// <param name="id">ID of Order Detail</param>
        /// <param name="oId">ID of Order contains Order Detail</param>
        /// <param name="detail">Detail about Service Package, Service Category, Service Category Title and Service Category Detail</param>
        /// <returns>
        /// <para>1     : successfully</para>
        /// <para>0     : Order contains this Order Detail has been done, you cannot update this Order Detail</para>
        /// <para>-99   : error</para>
        /// </returns>
        public int edit(string id, string oId, string detail)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                int? isUpdate = 0;
                var isSuccess = db.PC_OrderDetails_Upd(Int32.Parse(id), Int32.Parse(oId), detail, ref isUpdate);
                if (isUpdate.Value == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check update is valid or not]
        /// <summary>
        /// Check update is valid or not
        /// </summary>
        /// <param name="id">ID of Order Detail</param>
        /// <param name="oId">ID of Order contains this Order Detail</param>
        /// <returns>
        /// <para>1     : successfully</para>
        /// <para>2     : Order Detail ID is null or empty</para>
        /// <para>3     : Order Detail ID is not valid</para>
        /// <para>4     : Order ID is null or empty</para>
        /// <para>5     : Order ID is not valid</para>
        /// </returns>
        public int editValid(string id, string oId)
        {
            if (String.IsNullOrEmpty(id))
            {
                return 2;
            }

            Regex rId = new Regex("^[0-9]{0,}$");
            Match mId = rId.Match(id);
            if (!mId.Success)
            {
                return 3;
            }
            if (String.IsNullOrEmpty(oId))
            {
                return 4;
            }

            Regex rOId = new Regex("^[0-9]{0,}$");
            Match mOId = rOId.Match(oId);
            if (!mOId.Success)
            {
                return 5;
            }

            return 1;
        }
        #endregion

        #region [Delete an existed Order Detail]
        /// <summary>
        /// Delete an existed Order Detail
        /// </summary>
        /// <param name="id">ID of Order Detail</param>
        /// <param name="oId">ID of Order contains Order Detail</param>
        /// <returns>
        /// <para>1     : successfully</para>
        /// <para>0     : Order contains this Order Detail has been done, you cannot delete this Order Detail</para>
        /// <para>-99   : error</para>
        /// </returns>
        public int delete(string id, string oId)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                int? inDelete = 0;
                var isSuccess = db.PC_OrderDetails_Del(Int32.Parse(id), Int32.Parse(oId), ref inDelete);
                if (inDelete.Value == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion
    }
}